package com.zlict.admin.service.impl;

import com.zlict.admin.domain.ProjectApproval;
import com.zlict.admin.mapper.ProjectApprovalMapper;
import com.zlict.admin.service.IProjectApprovalService;
import com.zlict.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

import static com.zlict.common.utils.SecurityUtils.getUserId;

/**
 * 项目立项信息Service业务层处理
 * 
 * @author dzw
 * @date 2025-06-10
 */
@Service
public class ProjectApprovalServiceImpl implements IProjectApprovalService 
{
    @Autowired
    private ProjectApprovalMapper projectApprovalMapper;

    /**
     * 查询项目立项信息
     * 
     * @param id 项目立项信息主键
     * @return 项目立项信息
     */
    @Override
    public ProjectApproval selectProjectApprovalById(Long id)
    {
        //todo 关联上传的文件
        return projectApprovalMapper.selectProjectApprovalById(id);
    }
    /**
     * 查询项目立项信息
     *
     * @param projectId 项目主键
     * @return 项目立项信息
     */
    @Override
    public ProjectApproval selectProjectApprovalByProjectId(Long projectId)
    {
        //todo 关联上传的文件
        return projectApprovalMapper.selectProjectApprovalByProjectId(projectId);
    }

    /**
     * 查询项目立项信息列表
     * 
     * @param projectApproval 项目立项信息
     * @return 项目立项信息
     */
    @Override
    public List<ProjectApproval> selectProjectApprovalList(ProjectApproval projectApproval)
    {
        return projectApprovalMapper.selectProjectApprovalList(projectApproval);
    }

    /**
     * 新增项目立项信息
     * 
     * @param projectApproval 项目立项信息
     * @return 结果
     */
    @Override
    public int insertProjectApproval(ProjectApproval projectApproval)
    {
        checkAddProjectApproval(projectApproval);
        projectApproval.setCreateTime(DateUtils.getNowDate());
        projectApproval.setCreateBy(getUserId());
        return projectApprovalMapper.insertProjectApproval(projectApproval);
    }

    /**
     * 修改项目立项信息
     * 
     * @param projectApproval 项目立项信息
     * @return 结果
     */
    @Override
    public int updateProjectApproval(ProjectApproval projectApproval)
    {
        checkUpdateProjectApproval(projectApproval);
        //不让修改项目id
        projectApproval.setProjectId(null);



        projectApproval.setUpdateTime(DateUtils.getNowDate());
        projectApproval.setUpdateBy(getUserId());
        return projectApprovalMapper.updateProjectApproval(projectApproval);
    }

    /**
     * 批量删除项目立项信息
     * 
     * @param ids 需要删除的项目立项信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectApprovalByIds(Long[] ids)
    {
        return projectApprovalMapper.deleteProjectApprovalByIds(ids);
    }

    /**
     * 删除项目立项信息信息
     * 
     * @param id 项目立项信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectApprovalById(Long id)
    {
        return projectApprovalMapper.deleteProjectApprovalById(id);
    }


    //项目立项信息校验
    public boolean checkAddProjectApproval(ProjectApproval projectApproval) {
        if (ObjectUtils.isEmpty(projectApproval)) {
            throw new RuntimeException("未获取到参数");
        }
        if (ObjectUtils.isEmpty(projectApproval.getProjectId())) {
            throw new RuntimeException("为获取到项目");
        }
        if (projectApproval.getApplicant()==null) {
            throw new RuntimeException("请选择申请人");
        }
        if (ObjectUtils.isEmpty(projectApproval.getApplicantDate())) {
            throw new RuntimeException("请选择申请日期");
        }
        if (ObjectUtils.isEmpty(projectApproval.getStart())) {
            throw new RuntimeException("请选择项目开始日期");
        }
        if (ObjectUtils.isEmpty(projectApproval.getEnd())) {
            throw new RuntimeException("请选择项目结束日期");
        }
        if (ObjectUtils.isEmpty(projectApproval.getTarget())) {
            throw new RuntimeException("请选择主要技术经济目标");
        }
        if (ObjectUtils.isEmpty(projectApproval.getResearchContent())) {
            throw new RuntimeException("请填写项目主要研究内容");
        }
        if (ObjectUtils.isEmpty(projectApproval.getProjectExpect())) {
            throw new RuntimeException("请填写项目项目预期目标");
        }
        if (ObjectUtils.isEmpty(projectApproval.getTaskBook())) {
            throw new RuntimeException("请上传项目任务书");
        }
        if (ObjectUtils.isEmpty(projectApproval.getProjectProposal())) {
            throw new RuntimeException("请上传项目立项决议书");
        }
        return true;
    }

    public boolean checkUpdateProjectApproval(ProjectApproval projectApproval) {
        if (ObjectUtils.isEmpty(projectApproval)) {
            throw new RuntimeException("未获取到参数");
        }
        if (ObjectUtils.isEmpty(projectApproval.getId())) {
            throw new RuntimeException("未获取到项目立项信息id");
        }
        checkAddProjectApproval(projectApproval);
        return true;
    }
}
